import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

from Model.Project import Project
from Model.Scene import Scene
from View.ProjectExplorerWindow import ProjectExplorerWindow
from Controller.MainMenuController import MainMenuController
from View.MainMenuWindow import MainMenuWindow
from Model.Session import getSessionInstance
from Model.EditorPath import EditorPath
from View.SceneExplorerWindow import SceneExplorerWindow
from View.Widgets.CircularMenu import CircularMenu
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QTextEdit
from PyQt4.QtGui import QCursor
from PyQt4.QtGui import QImage
#from View.Widgets.MiniTextEntryWidget import MiniTextEntryWidget
from View import AppStyleSheet
from View.Widgets.EntityEditor.EntityWidget import *
from Model.Entity import Entity
from Model.Component import Component


class PyTextWidget(QTextEdit):
    
    def __init__(self, text, menu):
        QTextEdit.__init__(self, text)
        self.mMenu = menu
         
    def keyPressEvent(self, QKeyEvent):
        
        if QKeyEvent.key() == Qt.Key_A:
            if not self.mMenu.isVisible():
                self.mMenu.spawn(QCursor.pos().x(), QCursor.pos().y())


class ImageWidget(QtGui.QWidget):
    
    def __init__(self, menu):
        QtGui.QWidget.__init__(self)
        #self.setMinimumHeight(100)
        #self.setMinimumWidth(100)
        self.mImage = QImage()
        self.mImage.load(EditorPath.mImageFolderPath + EditorPath.mFakeImage)
        self.mMenu = menu
        self.mIsEnable = 0
        #self.grabKeyboard()
         
    def keyPressEvent(self, QKeyEvent):
        print("yo")
        if QKeyEvent.key() == Qt.Key_A:
            if not self.mMenu.isVisible():
                self.mMenu.spawn(QCursor.pos().x(), QCursor.pos().y())
        
    def paintEvent(self, QPaintEvent):
        
        qp = QtGui.QPainter()
        qp.begin(self)
        
        target = QtCore.QRectF( 0, 
                         0, 
                         self.width(), self.height()) 
        
        source = QtCore.QRectF(0, 
                        0, 
                        self.mImage.width(), 
                        self.mImage.height()) 
        
        if self.mIsEnable:
            qp.drawImage(target, self.mImage, source)
        
        qp.end()
        
    def leaveEvent (self, QEvent):
        self.releaseKeyboard()


    def enterEvent(self, *args, **kwargs):
        self.grabKeyboard()
        

        

class Panel(QtGui.QMainWindow):
    def __init__(self, title, parent=None):
        QtGui.QWidget.__init__(self, parent)
        

        self.setWindowTitle(title)
        self.resize(QtGui.QDesktopWidget().screenGeometry().width() / 2, QtGui.QDesktopWidget().screenGeometry().height() / 2)
        self.move(0, 0)

        self.mMdiArea = QtGui.QMdiArea();
        self.CreateMenu()
        
        #self.text = QtGui.QLabel()#PyTextWidget("Free3dk = #1 :D", self.mEntityMenu)
        self.mFakeImage = ImageWidget(self.mEntityMenu)
        self.setCentralWidget(self.mFakeImage)

        self.mMainMenuController = MainMenuController()
        self.mMainMenuWindow = MainMenuWindow(self, self.mMainMenuController)

        
    
        
        
        
        
        self.setupStyle()
        
        
    def enableImage(self):
        self.mFakeImage.mIsEnable = 1

    def CreateMenu(self):

        # Setup Project
        self.mPro = Project("")
        self.mSession = getSessionInstance()
        self.mSession.setProject(self.mPro)

        self.pp = ProjectExplorerWindow(self.mPro, self)

        self.addDockWidget(QtCore.Qt.RightDockWidgetArea , self.pp)
        
        
        ##################
        # Circular Menu
        ##################
        
        # Delete Image
        deleteImage = QImage()
        deleteImage.load(EditorPath.mImageFolderPath + EditorPath.mDeleteImageFile)
        
        deleteSelectedImage = QImage()
        deleteSelectedImage.load(EditorPath.mImageFolderPath + EditorPath.mDeleteSelectedImageFile)
        
        deletePressedImage = QImage()
        deletePressedImage.load(EditorPath.mImageFolderPath + EditorPath.mDeletePressedImageFile)
        
        
        # Entity Image
        entityProperty = QImage()
        entityProperty.load(EditorPath.mEntityImageFolderPath + EditorPath.mEntityPropertyImageFile)
        entityRotate = QImage()
        entityRotate.load(EditorPath.mEntityImageFolderPath + EditorPath.mEntityRotateImageFile)
        entityScale = QImage()
        entityScale.load(EditorPath.mEntityImageFolderPath + EditorPath.mEntityScaleImageFile)
        entityTranslate = QImage()
        entityTranslate.load(EditorPath.mEntityImageFolderPath + EditorPath.mEntityTranslateImageFile)
        entityZoom = QImage()
        entityZoom.load(EditorPath.mEntityImageFolderPath + EditorPath.mEntityZoomImageFile)
        
        # menu pies
        self.mEntityMenu = CircularMenu(200, 15, self)
        self.mEntityMenu.createImageMenu(entityProperty, 32, 32, "Property", entityProperty, entityProperty)
        self.mEntityMenu.createImageMenu(entityRotate, 32, 32, "Rotate", entityRotate, entityRotate)
        self.mEntityMenu.createImageMenu(entityScale, 32, 32, "Scale", entityScale, entityScale)
        self.mEntityMenu.createImageMenu(entityTranslate, 32, 32, "Translate", entityTranslate, entityTranslate)
        #self.mCircularMenu.createImageMenu(entityProperty, 32, 32, "Property", entityProperty, entityProperty)
        '''self.mEntityMenu.createTextMenu("Add entity")
        self.mEntityMenu.createTextMenu("Entity\nproperties")
        self.mEntityMenu.createTextMenu("Cancel")
        self.mEntityMenu.createTextMenu("Prout3")
        self.mEntityMenu.createTextMenu("Prout4")'''
        
        #self.mCircularMenu.createImageMenu(deleteImage, 64, 64, "Delete",deleteSelectedImage, deletePressedImage)

        # Setup Scene
        self.mScene = Scene()
        self.mSession.setScene(self.mScene)
        self.mSceneExplorerWindow = SceneExplorerWindow(self.mScene, self)
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.mSceneExplorerWindow)
        
        self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.pp)
        
        # Entity widget
        self.mEntityEditorScrollArea = QtGui.QScrollArea()
        self.mEntityDock = QtGui.QDockWidget("Entity editor")
        self.mEntityWidget = EntityWidget(self.mEntityEditorScrollArea, self.mEntityDock)
        self.mEntityEditorScrollArea.setWidget(self.mEntityWidget)
        
        self.mEntityDock.setWidget(self.mEntityEditorScrollArea)
        self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.mEntityDock)
        
        entity = self.buildDemoEntity1("Entity1")
        #self.mEntityWidget.setEntity(entity)
        self.mEntityDock.setMinimumWidth(300)
        
        
        
        

    def buildDemoEntity1(self, name="Entity"):
        entity = Entity("Entity1")
        
        component1 = Component("Material")
        entity.AddComponent(component1)
        
        component2 = Component("Mesh")
        entity.AddComponent(component2)
        
        component3 = Component("Transformation")
        entity.AddComponent(component3)
        
        return entity

    def AddMenu(self, name=""):

        self.menuBar().addMenu(name)
        
    def setupStyle(self):
        
        self.setStyleSheet(AppStyleSheet.mStyleSheet)
        
    def setEntity(self, entity):
        self.mEntityWidget.setEntity(entity)


def main():

        app = QtGui.QApplication(sys.argv)
        app.setApplicationName('3dkEditor')
        style = QtGui.QStyleFactory.create('cleanlooks')
        #print(QtGui.QStyleFactory.keys())
        app.setWindowIcon(QtGui.QIcon(EditorPath.mIconFolderPath + EditorPath.mAppIcoFile))
        app.setStyle(style)
        panel = Panel('3dkEditor')
        #panel.showFullScreen()
        panel.show()
        app.exec_()
        return 0

if __name__ == '__main__':
    main()
